using System;
using System.Collections.Generic;
using System.Text;

namespace SourceConverter.Components {
    public class ExtConfigCollection : List<ExtConfig> {
        public string ToExtSharp(ExtClass ec) {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("    public class {0}Config ", ec.Name);
            sb.AppendLine("{");
            sb.AppendLine("        protected Dictionary o = new Dictionary();");
            sb.AppendLine();
            foreach (ExtConfig ev in this) {
                sb.AppendLine(ev.ToExtSharp(ec.Name + "Config"));
            }

            sb.AppendLine(SourceConverter.GetMissingCode(ec.FullyQualifiedName + "Config"));

            sb.AppendLine("        /// <summary>Allows you to add custom config options</summary>");
            sb.AppendLine("        public " + ec.Name + "Config custom(string key, object value) {");
            sb.AppendLine("            o[key] = value;");
            sb.AppendLine("            return this;");
            sb.AppendLine("        }");
            sb.AppendLine();
            sb.AppendLine("        public Dictionary ToDictionary() { return o; }");
            sb.AppendLine("    }");
            return sb.ToString();
        }

        public new void Add(ExtConfig ec) {
            if (ec.Name == "cm" && ec.Type == "{Store}") return; // incorrect documentation in GridPanel
            bool found = false;
            ForEach(delegate(ExtConfig c) { if (c.Name == ec.Name) found = true; });
            if (!found) base.Add(ec);
        }

        public void CopyBaseConfigs(ExtClass ec, ExtClassCollection classes) {
            ExtClass baseClass = classes.FindClass(ec.SuperClass); 
            if (baseClass != null) {
                baseClass.Configs.CopyBaseConfigs(baseClass, classes);
                foreach (ExtConfig config in baseClass.Configs) {
                    Add(config.Dupe());
                }
            }
        }
    }
}
